package com.teamall.web.main.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.teamall.core.common.BaseController;
import com.teamall.web.tssy.db.po.TsSyUserPO;

public class SessionInterceptor extends HandlerInterceptorAdapter {

	private List<String> excludedUrls;

	public List<String> getExcludedUrls() {
		return excludedUrls;
	}

	public void setExcludedUrls(List<String> excludedUrls) {
		this.excludedUrls = excludedUrls;
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String requestUri = request.getRequestURI();
		for (String url : excludedUrls) {
			if (requestUri.endsWith(url)) {
				return true;
			}
		}
		TsSyUserPO userInfo = (TsSyUserPO) request.getSession().getAttribute(BaseController.LOGIN_SESSION_KEY);
		String webPath = request.getContextPath();
		if ("/".equals(webPath)) {
			webPath = "";
		}
		if (userInfo == null) {
			response.sendRedirect(webPath + "/logout");
			return false;
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}
}
